<template>
  <div class="user-tel">
		<el-page-header @back="goBack" content="验证当前手机号"></el-page-header>
    <!-- 验证旧手机号 -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone"  :disabled="true"></el-input>
      </el-form-item>

      <el-form-item label="验证码" prop="code">
       <el-input v-model="ruleForm.code" placeholder="请输入验证码">
       	  <template slot="append">
            <el-button  v-no-more-click:3000
              :disabled="btnIsDisabled"
              class="btn-code"
              type="primary"
              @click="getVCode()">{{VCodeText}}
            </el-button>
          </template>
       </el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-no-more-click:3000 type="primary" @click="verifyPhone('ruleForm')">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "verifyPhone",
  data() {
    return {
      ruleForm: {},
      rules: {
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      },
      VCodeText:"获取验证码",
      btnIsDisabled: false,
    };
  },
  created () {
    this.getUserTel();

  },
  // beforeRouteUpdate (to, from, next) {
  //   // 在当前路由改变，但是该组件被复用时调用 可以访问组件实例 `this`
  //   // 举例来说，对于一个带有动态参数的路径 /foo/:id，在 /foo/1 和 /foo/2 之间跳转的时候，
  //   // 由于会渲染同样的 Foo 组件，因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
  //   this.$confirm('修改手机号会更改', '确认信息', {
  //     distinguishCancelAndClose: true,
  //     confirmButtonText: '保存',
  //     cancelButtonText: '放弃修改'
  //   })
  // },

  methods: {
    getUserTel(){
      this.ruleForm.phone = this.$store.state.user.phone
    },
    goBack() {
      this.$router.go(-1);
    },
    //验证码
    getVCode() {
      let that = this;
      if (/^1[3456789]\d{9}$/.test(that.ruleForm.phone)) {
        that.$axios
          .post(
            "/api/captcha",{
              phone: that.ruleForm.phone,
              type: 2
            })
          .then(res => {
            if (res.data.code == 200) {
              that.$message({
                message: "验证码发送成功！",
                type: "success"
              });
                let num = 60;
                that.btnIsDisabled = true;
                let timer = setInterval(function() {
                  that.VCodeText = num + "秒后可重新发送";
                  num -= 1;
                  if (num < 0) {
                    that.VCodeText = "获取验证码";
                    that.btnIsDisabled = false;
                    clearInterval(timer);
                  }
                }, 1000);
            } else {
              if (res.data.code == 400) {
                that.$message({
                  message: res.data.message,
                  type: "error"
                });
              } else {
                that.$message({
                  message: "验证码发送失败！",
                  type: "error"
                });
              }
            }
          });
      } else {
        that.$message({
          message: "手机号不能为空且格式不能有误",
          type: "warning"
        });
      }
    },

    //检验旧手机号
    verifyPhone(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios
            .get(
              "api/consumer/verifyPhone?phone=" +
                this.ruleForm.phone.toString() +
                "&code=" +
                this.ruleForm.code.toString()
            )
            .then(res => {
              if (res.data.code == 200) {
                  this.$store.state.isUpdateTel = true; //已经过旧手机验证
                  this.$router.push({path:'/user/set/updateTel'})
              } else {
                this.$message.error(res.data.message);
              }
            })
            .catch(err => {
              console.log(err);
            });
        } else {
          this.$message.error("请按要求填写手机号码及验证码");
          return false;
        }
      });
      // this.$router.push({path:'/user/set/updateTel'})
    },

  },
  // beforeRouteEnter (to, from, next) {
  //   next ( vm => {
  //     vm.$confirm('<span style="font-size: 16px;">修改手机号后，您之前所有订单联系人手机号都会更改为此手机号！</span>', '提示', {
  //       confirmButtonText: '我知道了',
  //       cancelButtonText:'取消',
  //       confirmButtonClass: 'iKonw',
  //       dangerouslyUseHTMLString: true,
  //       type: 'warning',
  //     }).then(() => {
        
  //     }).catch(() => {
  //       from.path === '/' ? vm.$router.push('/user/myOrder/orderInProcess') : vm.$router.push(from.path)
  //     });
  //   })
  // },
};
</script>
<style lang="less">
.user-tel {
  .el-form {
    width: 800px;
    margin: 80px 0 0 50px;
		.el-input {
		  width: 300px;
		}
    .btn-code {
      padding: 13px 20px ;
      float: right;
      color: #FFF;
      background-color: #409EFF;
      border-color: #409EFF;
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
      span {
        min-width: 115px;
        display: block;
      }
    }
  }
}
// .el-button {
 
// }
.iKonw {
  padding: 10px;
  // color: 
}
</style>